package top.braycep.task;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import top.braycep.ui.AboutUI;
import top.braycep.ui.MainUI;
import top.braycep.utils.AppUtils;
import top.braycep.utils.UpdateUtils;

import javax.swing.*;

public class CheckUpdateTask extends SwingWorker {
    private static final Log LOG = LogFactory.get(CheckUpdateTask.class);
    private MainUI mainUI;
    private JMenuItem menuItem;

    public CheckUpdateTask(MainUI mainUI, JMenuItem menuItem) {
        this.mainUI = mainUI;
        this.menuItem = menuItem;
    }

    @Override
    protected Object doInBackground() throws Exception {
        AboutUI.hasNew = UpdateUtils.hasUpdate();

        menuItem.setVisible(AboutUI.hasNew);

        return UpdateUtils.getVersionInfo();
    }

    @Override
    protected void done() {
        if (AboutUI.hasNew) {
            AppUtils.showInfo(mainUI, "提示", "发现新版本：v" + UpdateUtils.getVersionInfo().getVersionNo());
        } else {
            AppUtils.showInfo(mainUI, "提示", "当前已是最新版本");
        }
    }
}
